package com.minimalist.basic.controller.lindi;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.contracttemplate.LindiContractTemplateVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationcontract.LindiTradeApplicationContractQueryVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationcontract.LindiTradeApplicationContractVO;
import com.minimalist.common.feign.client.lindi.LindiTradeApplicationContractFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.SafetyUtil;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 林地指标交易平台_交易申请合同控制器
 *
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "林地指标交易平台_交易申请合同管理")
@RequestMapping("/basic/lindi/tradeApplicationContract")
public class LindiTradeApplicationContractController{

    @Autowired
    private LindiTradeApplicationContractFeignClient feignClient;

    @PostMapping("/addTradeApplicationContract")
    @Operation(summary = "添加交易申请合同")
    @SaCheckPermission("lindi:tradeApplicationContract:add")
    public R<?> addTradeApplicationContract(
            @RequestBody @Validated(Add.class) LindiTradeApplicationContractVO tradeApplicationContractVO){
        tradeApplicationContractVO.setCreateId(StpUtil.getLoginIdAsLong());
        tradeApplicationContractVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        tradeApplicationContractVO.setCreateTime(now);
        tradeApplicationContractVO.setUpdateTime(now);
        return feignClient.addTradeApplicationContract(tradeApplicationContractVO);
    }

    @DeleteMapping("/deleteTradeApplicationContractByTradeApplicationContractId")
    @Operation(summary = "删除交易申请合同 -> 根据交易申请合同ID删除")
    @SaCheckPermission("lindi:tradeApplicationContract:delete")
    public R<?> deleteTradeApplicationContractByTradeApplicationContractId(
            @RequestParam("tradeApplicationContractId")
            @NotNull(message = "交易申请合同ID不能为空")
            @Parameter(name = "tradeApplicationContractId", required = true, description = "交易申请合同ID")
            Long tradeApplicationContractId){
        return feignClient.deleteTradeApplicationContractByTradeApplicationContractId(tradeApplicationContractId);
    }

    @PutMapping("/updateTradeApplicationContractByTradeApplicationContractId")
    @Operation(summary = "修改交易申请合同")
    @SaCheckPermission("lindi:tradeApplicationContract:update")
    public R<?> updateTradeApplicationContractByTradeApplicationContractId(
            @RequestBody @Validated(Update.class) LindiTradeApplicationContractVO tradeApplicationContractVO){
        tradeApplicationContractVO.setUpdateId(StpUtil.getLoginIdAsLong());
        tradeApplicationContractVO.setUpdateTime(LocalDateTime.now());
        return feignClient.updateTradeApplicationContractByTradeApplicationContractId(tradeApplicationContractVO);
    }

    @GetMapping("/getPageTradeApplicationContractList")
    @Operation(summary = "查询交易申请合同列表(分页)")
    @SaCheckPermission("lindi:tradeApplicationContract:get")
    public R<PageResp<LindiTradeApplicationContractVO>> getPageTradeApplicationContractList(
            LindiTradeApplicationContractQueryVO queryVO){
        return feignClient.getPageTradeApplicationContractList(queryVO);
    }

    @GetMapping("/getTradeApplicationContractByTradeApplicationContractId/{tradeApplicationContractId}")
    @Operation(summary = "根据交易申请合同ID查询交易申请合同")
    @SaCheckPermission("lindi:tradeApplicationContract:get")
    public R<LindiTradeApplicationContractVO> getTradeApplicationContractByTradeApplicationContractId(
            @PathVariable(value = "tradeApplicationContractId")
            @NotNull(message = "交易申请合同ID不能为空")
            @Parameter(name = "tradeApplicationContractId", description = "交易申请合同ID", required = true)
            Long tradeApplicationContractId){
        return feignClient.getTradeApplicationContractByTradeApplicationContractId(tradeApplicationContractId);
    }

    @GetMapping("/getTradeApplicationContractByTradeApplicationId/{tradeApplicationId}")
    @Operation(summary = "根据交易申请ID查询合同信息")
    @SaCheckPermission("lindi:tradeApplicationContract:get")
    public R<LindiTradeApplicationContractVO>
    getTradeApplicationContractByTradeApplicationId(
            @PathVariable(value = "tradeApplicationId")
            @NotNull(message = "交易申请ID不能为空")
            @Parameter(name = "tradeApplicationId", description = "交易申请ID", required = true)
            Long tradeApplicationId){
        return feignClient.getTradeApplicationContractByTradeApplicationId(tradeApplicationId);
    }

    @GetMapping("/getTradeApplicationContractListByTradeApplicationId/{tradeApplicationId}")
    @Operation(summary = "根据交易申请ID查询交易申请合同列表")
    @SaCheckPermission("lindi:tradeApplicationContract:get")
    public R<List<LindiTradeApplicationContractVO>>
    getTradeApplicationContractListByTradeApplicationId(
            @PathVariable(value = "tradeApplicationId")
            @NotNull(message = "交易申请ID不能为空")
            @Parameter(name = "tradeApplicationId", description = "交易申请ID", required = true)
            Long tradeApplicationId){
        return feignClient.getTradeApplicationContractListByTradeApplicationId(tradeApplicationId);
    }

    @GetMapping("/getContractTemplateList")
    @SaCheckPermission("lindi:tradeApplication:get")
    @Operation(summary = "获取合同模板列表")
    public R<List<LindiContractTemplateVO>> getContractTemplateList(){
        return feignClient.getContractTemplateList();
    }

    @PostMapping("/generateContract")
    @Operation(summary = "生成合同")
    @SaCheckPermission("lindi:tradeApplicationContract:add")
    public R<LindiTradeApplicationContractVO> generateContract(
            @RequestParam("tradeApplicationId")
            @NotNull(message = "交易申请ID不能为空")
            @Parameter(name = "tradeApplicationId", required = true, description = "交易申请ID")
            Long tradeApplicationId,
            @RequestParam("contractTemplateId")
            @NotNull(message = "合同模板ID不能为空")
            @Parameter(name = "contractTemplateId", required = true, description = "合同模板ID")
            Long contractTemplateId){
        Long createId = StpUtil.getLoginIdAsLong();
        Long tenantId = SafetyUtil.getLoginUserTenantId();
        return feignClient.generateContract(tradeApplicationId, contractTemplateId
                , createId, tenantId);
    }
}